package db

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql" //mysql driver
	"log"
)

var db *sql.DB

const (
	driverName     = "mysql"
	dataSourceName = "root:123456@tcp(localhost:3306)/user_cn_db?charset=utf8mb4"
	maxOpenConn    = 4000
	maxIdleConn    = 2000
)

// InitDB init db
func InitDB() {
	var err error
	db, err = sql.Open(driverName, dataSourceName)
	if err != nil {
		log.Fatalf("Failed to connect to db '%s', err: %v", dataSourceName, err)
	}

	db.SetMaxOpenConns(maxOpenConn)
	db.SetMaxIdleConns(maxIdleConn)
	//db.SetConnMaxLifetime(2)
}

// CloseDB close db
func CloseDB() {
	db.Close()
}
